<template>
   <div class="app-container"><h4>每收到一笔入金，立即以下面的比率划转到以下账号里</h4>
   <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['bizmanage:set:directly']"
        >新增</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="receiverList">
      <el-table-column label="手机号" align="center" prop="receiverPhone" />
      <!-- <el-table-column label="分配比率" align="center" prop="percent" :show-overflow-tooltip="true" /> -->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['bizmanage:set:directly']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['bizmanage:set:directly']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改终端对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="手机号" prop="receiverPhone">
          <el-input v-model="form.receiverPhone" placeholder="请输入手机号" disabled v-if="isDelete"/>
          <el-input v-model="form.receiverPhone" placeholder="请输入手机号" v-else/>
        </el-form-item>
        <el-form-item label="分配比率" prop="percent">
          <el-input v-model="form.percent" placeholder="请输入百分比，整数"  disabled v-if="isDelete"/>
          <el-input v-model="form.percent" placeholder="请输入百分比，整数"  v-else/>
        </el-form-item>
        <el-form-item>
          <el-button type="primary"  @click="sendCaptchaCode" v-if="countDown === '发送验证码'">{{countDown}}</el-button>
          <el-button type="primary"  disabled v-else>{{countDown}}</el-button>
        </el-form-item>
        <el-form-item label="验证码" prop="captcha">
          <el-input v-model="form.captcha" placeholder="为了资金安全，请输入您收到手机验证码" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { addReceiver, updateReceiver, delReceiver, listReceiver } from "@/api/bizmanage/set";
import { getPhoneCode } from "@/api/login";

export default {
  name: "Client",
  data() {
    return {
      // 验证码文本
      countDown:'发送验证码',
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 终端表格数据
      receiverList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        clientId: undefined
      },
      // 是否新增
      isAdd: false,
      isDelete: false,
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        receiverPhone: [
          { required: true, message: "手机号格式错误", trigger: "blur", pattern: /^1(3|4|5|7|8|9)\d{9}$/ }
        ],
        percent: [
          { required: true, message: "分配比率长度为1～2的整数", trigger: "blur", pattern: /^\d{1,2}$/ }
        ],
        captcha: [
          { required: true, message: "验证码是长度为6的整数", trigger: "blur", pattern: /^\d{6}$/ }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询终端列表 */
    getList() {
      this.loading = true;
      
      listReceiver(this.$store.state.user['name']).then(response => {
        this.receiverList = response.data;
        this.total = response.data.length
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },// 表单重置
    reset() {
      this.form = {
        receiverPhone: undefined,
        percent: undefined,
        captcha: undefined
      };
      this.resetForm("form");
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.isAdd = true;
      this.isDelete = false;
      this.title = "添加";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {      
      this.reset();
      this.open = true;
      this.isAdd = false;
      this.isDelete = false;
      this.title = "修改";
      this.form.receiverPhone = row.receiverPhone
      this.form.percent = row.percent
    },
    /** 修改按钮操作 */
    handleDelete(row) {   
      this.reset();
      this.open = true;
      this.isDelete = true;
      this.title = "删除";
      this.form.receiverPhone = row.receiverPhone
      this.form.percent = row.percent
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.isDelete) {
            delReceiver({'phone':this.$store.state.user['name'],'captcha':this.form.captcha,'produceType':2,'receiverPhone':this.form.receiverPhone}).then(response => {
              if (response.code === 1) {
                this.msgSuccess("删除成功");
                this.open = false;
                this.getList();
              }
            });
          } else if (!this.isAdd) {
            updateReceiver({'phone':this.$store.state.user['name'],'captcha':this.form.captcha,'produceType':2,'receiverPhone':this.form.receiverPhone,'percent':this.form.percent}).then(response => {
              if (response.code === 1) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              }
            });
          } else {            
            addReceiver({'phone':this.$store.state.user['name'],'captcha':this.form.captcha,'produceType':2,'receiverPhone':this.form.receiverPhone,'percent':this.form.percent})
            .then(response => {
              if (response.code === 1) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              }
            }).catch((err) =>{
              console.log(err);
              // alert(err);
            });
          }
        }
      });
    },
    sendCaptchaCode(){
      getPhoneCode(this.$store.state.user['name']);
      this.startSendCaptchaCode()
    },
    startSendCaptchaCode () {
      this.countDown = 60
      this.countDownTimer = setInterval(() => {
        this.countDown -= 1
        if (this.countDown <= 0) {
          this.stopSendCaptchaCode()
        }
      }, 1000)
    },
    stopSendCaptchaCode () {
      this.countDown = "发送验证码"
      if (this.countDownTimer) {
        clearInterval(this.countDownTimer)
        this.countDownTimer = null
      }
    },
  }
};

</script>
<style>
</style>
